package com.atguigu.gmall.product.rest;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-08-16 10:06
 */
@RequestMapping("/admin/product")
@RestController
public class SpuController {


    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    /**
     * 分页查询SPU
     * @param category3Id
     * @return
     */
    @GetMapping("/{pn}/{ps}")
    public Result spuList(@RequestParam("category3Id") Long category3Id,
                          @PathVariable("pn") Long pn,
                          @PathVariable("ps") Long ps){

        //lambdaQuery 分页查询
        Page<SpuInfo> page = spuInfoService.lambdaQuery()
                .eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<>(pn, ps));

        return Result.ok(page);
    }


    /**
     * Spu大保存
     * @param vo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SaveSpuVo vo){

        spuInfoService.saveSpuInfo(vo);

        return Result.ok();
    }


    /**
     * 获取这个spu当时上传的所有图片
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuImageService.lambdaQuery()
                .eq(SpuImage::getSpuId, spuId)
                .list();

        return Result.ok(list);
    }






}
